<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/19
 * Time: 21:33
 */

namespace Admin\Logic;
use Think\Model;

class ItemLogic extends Model
{
    protected $trueTableName = 'wz_item';

    /**
     * @param $params
     * @return string
     */
    public function saveItem($params){
        $id = isset($params['id'])?intval($params['id']):'';
        if(!$id){
            $result = D('Resource/Item','Service')->addItem($params);
        }else{
            $result = D('Resource/Item','Service')->updateItemInfo($params);
        }

        if(!$result){
            return Response(2001,"操作失败");
        }
        return Response(999,"操作成功");
    }

    /**
     * @param $params
     * @return mixed
     */
    public function searchItemList($params) {
        $result = D('Resource/Item', 'Service')->searchItemList($params);
        if(is_array($result['itemList']) && count($result['itemList'])){
            foreach ($result['itemList'] as $k=>$item){
                $result['itemList'][$k]['parent_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['parent_cid']]);
                $result['itemList'][$k]['second_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['second_cid']]);
                $result['itemList'][$k]['three_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['three_cid']]);
            }
        }
        return $result;
    }
    /**
     * @param $params
     * @return string
     */
    public function deleteItem($params){
        $id = isset($params['id'])?intval($params['id']):'';
        $status = isset($params['status'])?intval($params['status']):-5;

        if(!$id) {
            return Response(2002, "参数异常");
        }
        $data = [];
        $data['id'] = $id;
        $data['status'] = $status;

        $result = D('Resource/Item','Service')->updateItemInfo($data);

        if(!$result){
            return Response(2001,"操作失败");
        }
        return Response(999,"操作成功");
    }

    /**
     * @param $params
     * @return array
     */
    public function getAllItemList($params){
        $itemList = D('Resource/Item','Service')->getAllItemList($params);
        if(is_array($itemList) && count($itemList)>0){
            foreach ($itemList as $k=>$item){
                $itemList[$k]['parent_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['parent_cid']]);
                $itemList[$k]['second_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['second_cid']]);
                $itemList[$k]['three_name'] = D('Resource/ItemCate','Service')->getCateName(['id'=>$item['three_cid']]);
            }
        }
        return $itemList;
    }
}